package com.wpw.web;

import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @Description:
 * @ClassName: Test1
 * @CreateDate: 2019-12-01 22:50
 * @author: wupengwei
 */
public class Test1 {

    static class OOMObj {
        public byte[] p = new byte[1024 * 1024];

    }

    public static void main(String[] args) {
        //fillHeap(150);
        //deathLock();
        int i = 4>>1;
        System.out.println(i);
    }

    public static void fillHeap(int num) {
//        byte[] bytes = new byte[64 * 1024];
        List<OOMObj> ooList = new ArrayList<>();
        for (int i = 0; i < num; i++) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(i);

            ooList.add(new OOMObj());
        }
    }

    public static void deathLock() {
        final Lock lock1 = new ReentrantLock();
        final Lock lock2 = new ReentrantLock();

        Thread t1 = new Thread() {
            @Override
            public void run() {
                try {
                    lock1.lock();
                    TimeUnit.SECONDS.sleep(1);
                    lock2.lock();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };
        Thread t2 = new Thread() {
            @Override
            public void run() {
                try {
                    lock2.lock();
                    TimeUnit.SECONDS.sleep(1);
                    lock1.lock();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };
        t1.setName("mythread1");
        t2.setName("mythread2");
        t1.start();
        t2.start();
    }

}